# -*- coding: utf-8 -*-
import cv2
import os
import time
import numpy as np


def open_camera():
  cap = cv2.VideoCapture(0)
  while True:
    flag, frame = cap.read()
    if not flag:
        break
    cv2.imshow('camera',frame)
    key = cv2.waitKey(1)  #默认-1
    if key > -1:
        cv2.destroyAllWindows()
        break
    print("key=",key)

def open_camera2():
  cap = cv2.VideoCapture(0)
  while True:
    flag, frame = cap.read()
    if not flag:
        break
    cv2.imshow('camera',frame)
    key = cv2.waitKey(10)  #默认-1
    if key == ord('q'):
        break
    elif key == ord('s'):
        cv2.imwrite('save_{}.png'.format(time.time()),frame)
    print("key=",key)

def img_method():
    img = cv2.imread('img/jihetuxing.png')
    # 灰度图
    # img = cv2.imread('img/dhhead.png', cv2.IMREAD_GRAYSCALE)
    #图像尺寸
    print("img shape",img.shape)
    cv2.imshow("img",img)
    cv2.waitKey(0)
    roi(img)

def bound():
    img = cv2.imread('img/jihetuxing.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
    binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = contours[3]
    x,y,w,h = cv2.boundingRect(cnt)
    img = cv2.rectangle(gray,(x,y),(x+w,y+h),(0,255,0),2)
    print(x,y,w,h,len(contours))
    cv2.imshow('img',img)
    cv2.waitKey(0)

def roi(img):
  print(img.shape)
  ret = img[0:200,0:200]
  cv2.imshow("roi",ret)
  cv2.waitKey(0)

def bord_camera():
  cap = cv2.VideoCapture(0)
  while True:
    flag, frame = cap.read()
    if not flag:
        break
    borde = cv2.pyrUp(cv2.pyrDown(frame))-frame
    borde2 = cv2.pyrDown(cv2.pyrUp(frame))-frame
    cv2.imshow("borde",np.hstack((borde,borde2)))
    key = cv2.waitKey(100)  #默认-1
    if key == ord('q'):
        break
    elif key == ord('s'):
        cv2.imwrite('save_{}.png'.format(time.time()),frame)
    print("key=",key)

if __name__=="__main__":
    # open_camera2()
    # img_method()
    # bord_camera()
    a = 1
    b=1
    a2 = 2
    b2 = 2
    c = 127
    d = 127
    e = 128
    f = 129
    
    print(id(a),id(b))
    print(id(a2),id(b2))
    print(id(c),id(d))
    print(id(e),id(f))